package me.xplabs.fight.layer 
{
	import flash.display.BitmapData;
	import me.xplabs.engine.display.BaseDisplay;
	import me.xplabs.engine.display.BaseDisplayContainer;
	import me.xplabs.event.MMouseEvent;
	import me.xplabs.flag.FlagDisplay;
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.MFlagInfo;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.SimpleObjPool;
	
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class FlagLayer extends BaseDisplayContainer 
	{
		private var _flagPool:SimpleObjPool;
		private var _flags:Vector.<FlagDisplay>;
		public function FlagLayer() 
		{
			super();
			_flagPool = new SimpleObjPool(FlagDisplay, 1, 1);
			_flags = new Vector.<FlagDisplay>();
			isSort = true;
			//mouseChild = true;
		}
		
		
		override protected function sort(p1:BaseDisplay, p2:BaseDisplay):int 
		{
			return p1.y - p2.y;
		}
		public function addFlag(p_flag:MFlagInfo):void
		{
			//var tempFlag:FlagDisplay = new FlagDisplay();
			var tempFlag:FlagDisplay = _flagPool.getObj();
			tempFlag.soleId = p_flag.soleId;
			tempFlag.flagInfo = p_flag;
			//tempFlag.mouseEnable = true;
			//tempFlag.mouseChild = true;
			tempFlag.buttonMode = true;
			tempFlag.addEventListener(MMouseEvent.ROLL_OVER, flagRollOverHandler);
			tempFlag.addEventListener(MMouseEvent.ROLL_OUT, flagRollOutHandler);
			//tempFlag.addEventListener(MMouseEvent.CLICK, actorClickHandler);
			//tempFlag.addEventListener(MMouseEvent.MOUSE_DOWN, actorMouseDownHandler);
			//tempFlag.addEventListener(MMouseEvent.MOUSE_UP, actorMouseUpHandler);
			//tempFlag.addEventListener(MMouseEvent.DOUBLE_CLICK, actorDoubleClickHandler);
			tempFlag.resLib[1] = [];
			//tempFlag.resLib[1].push(Globals.lib.getResObj(Globals.domain.texture + 301));
			//var resBitmapData:* = Globals.lib.getResObj(Globals.domain.magic +"r" + 3 + ".i1");
			var tempType:int = p_flag.flagType;
			if (p_flag.flagType == 3) tempType = 2;
			else if (p_flag.flagType == 2) tempType = 3;
			var resBitmapData:* = Globals.lib.getResObj(Globals.domain.magic +"r" + (4 + tempType) + ".i1");
			//trace("旗帜的类型=" + p_flag.flagType);
			if (resBitmapData)
			{
				tempFlag.resLib[1].push(resBitmapData);
				var len:int = resBitmapData.length;
				for (var i:int = 1; i < len; i++) 
				{
					//tempFlag.resLib[1].push(Globals.lib.getResObj(Globals.domain.magic +"r" + 3 + ".i" + (i + 1)));
					tempFlag.resLib[1].push(Globals.lib.getResObj(Globals.domain.magic +"r" + (4 + tempType) + ".i" + (i + 1)));
				}
			}
			tempFlag.setAnimate(100, true, len);
			//tempFlag.resLib[1].push(Globals.lib.getResObj(Globals.domain.build +"i." + builds[i].resId));
				/*for (var j:int = 0; j < 4; j++) 
				{
					tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.magic + "r100010_4" + ".i" + j));
				}*/
			addChild(tempFlag);
			_flags.push(tempFlag);
		}
		
		private function flagRollOutHandler(e:MMouseEvent):void 
		{
			var flag:FlagDisplay = FlagDisplay(e.currentTarget);
			print("移出旗帜：" + flag.soleId);
			sendMsg(NotificationName.ROLL_OUT_FLAG);
		}
		
		private function flagRollOverHandler(e:MMouseEvent):void 
		{
			var flag:FlagDisplay = FlagDisplay(e.currentTarget);
			print("移入旗帜：" + flag.soleId);
			sendMsg(NotificationName.ROLL_OVER_FLAG, { pw:flag.flagInfo.areaW, ph:flag.flagInfo.areaH, x:flag.flagInfo.curPosX, y:flag.flagInfo.curPosY } );
		}
		public function delFlag(p_soleId:int):void
		{
			var len:int = _flags.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_flags[i].flagInfo.soleId == p_soleId)
				{
					_flags[i].dispose();
					//_flags[i].removeEventListener(MMouseEvent.CLICK, actorClickHandler);
					_flags[i].removeEventListener(MMouseEvent.ROLL_OVER, flagRollOverHandler);
					_flags[i].removeEventListener(MMouseEvent.ROLL_OUT, flagRollOutHandler);
					if (_flags[i].parent) _flags[i].parent.removeChild(_flags[i]);
					_flagPool.dispose(_flags.splice(i, 1)[0]);
					break;
				}
			}
		}
		
	}

}